package com.mk.admin.api;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mk.admin.model.entity.ApiEntity;
import com.mk.admin.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 项目启动后默认执行代码
 * 查询数据库得到当前当前用户信息
 * 用于得到token和平台调用接口
 */
@Component
public class Api {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ApiService apiService;
    public static ApiEntity apiEntity = null;

    @PostConstruct
    private void checkApiFromSql() {
        LambdaQueryWrapper<ApiEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(ApiEntity::getName, "郫都区医院");
        lambdaQueryWrapper.eq(ApiEntity::getStatus, 1);
        List<ApiEntity> list = apiService.list(lambdaQueryWrapper);
//        System.out.println(list);
        if (list.size() > 0) {
            redisTemplate.opsForValue().set("hospitalUrlApi", JSONObject.toJSONString(list.get(0)));
        }
        Object hospitalUrlApi = redisTemplate.opsForValue().get("hospitalUrlApi");
        apiEntity = JSONObject.parseObject(hospitalUrlApi != null ? hospitalUrlApi.toString() : null, ApiEntity.class);
    }

}
